﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;

namespace TMS
{
    public partial class StocktakingReportControl : UserControl
    {
        Product prod = new Product();
        DataSet Products = new DataSet();
        Stocktaking NewStocktaking = new Stocktaking();
        Users ActiveUser = Program.GetActiveUser;
        DateTime Today = DateTime.Today;
        bool print = false;

        string[] row;

        public StocktakingReportControl()
        {
            InitializeComponent();
        }

        public StocktakingReportControl(DataGridViewRow parameters)
        {
            Int32 ProductCounter = 1;
            NewStocktaking.ID=Convert.ToInt32(parameters.Cells[0].Value);
            NewStocktaking.Date = Convert.ToDateTime(parameters.Cells[1].Value);
            InitializeComponent();
            Program.GetMainForm.Text = "Попис " + Convert.ToDateTime(parameters.Cells[1].Value).ToString("dd-MMM-yyyy");
            dgvStockatakingReportList.ColumnCount = 10;
            dgvStockatakingReportList.Columns[0].Visible = false;
            dgvStockatakingReportList.Columns[1].Visible = false;
            dgvStockatakingReportList.Columns[2].Visible = false;
            dgvStockatakingReportList.Columns[3].HeaderText = "Бр.";
            dgvStockatakingReportList.Columns[4].HeaderText = "Пр. бр.";
            dgvStockatakingReportList.Columns[5].HeaderText = "Име";
            dgvStockatakingReportList.Columns[6].HeaderText = "Опис";
            dgvStockatakingReportList.Columns[7].HeaderText = "Стара состојба";
            dgvStockatakingReportList.Columns[8].HeaderText = "Нова Состојба";
            dgvStockatakingReportList.Columns[9].HeaderText = "Разлика";

            if (parameters.Cells[2].Value.ToString() == "отворен")
            {
                Products = prod.GetAllProductsFromDB();

                foreach (DataRow Product in Products.Tables[0].Rows)
                {
                    Int32 CurentQuantity = prod.GetProductQuantityFromWarehouse(Convert.ToInt32(Product[0]));
                    Int32 TakenQuantity = prod.GetProductFromStocktaking(Convert.ToInt32(Product[0]), NewStocktaking.ID);
                    if ((CurentQuantity > 0) || (TakenQuantity > 0))
                    {
                        row = new string[] { "", parameters.Cells[0].Value.ToString(), Product[0].ToString(), ProductCounter.ToString(), Product[5].ToString(), Product[4].ToString(), Product[10].ToString(), CurentQuantity.ToString(), TakenQuantity.ToString(), (TakenQuantity - CurentQuantity).ToString() };
                        dgvStockatakingReportList.Rows.Add(row);
                        ProductCounter++;
                    }

                }
            }
            else
            {
                ToStClose.Visible = false;
                DataSet table = NewStocktaking.GetStocktakingReport();
                foreach (DataRow row in table.Tables[0].Rows)
                {
                    try
                    {
                        DataSet Product = prod.GetProductFromDBAsDataSet(Convert.ToInt32(row.ItemArray[2]));
                        dgvStockatakingReportList.Rows.Add(new object[] { row.ItemArray[0].ToString(), row.ItemArray[1].ToString(), row.ItemArray[2].ToString(), row.ItemArray[6].ToString(), Product.Tables[0].Rows[0].ItemArray[5].ToString(), Product.Tables[0].Rows[0].ItemArray[4].ToString(), Product.Tables[0].Rows[0].ItemArray[10].ToString(), row.ItemArray[3].ToString(), row.ItemArray[4].ToString(), row.ItemArray[5].ToString() });
                    }
                    catch
                    {
                        MessageBox.Show("Продуктот со ID" + row.ItemArray[2].ToString() + "е избришан од продукти");
                    }
                }
            }

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            if (ActiveUser.GetName() == "Администратор")
            {
                NewStocktaking.CloseStocktaking();
                foreach (DataGridViewRow row in dgvStockatakingReportList.Rows)
                {
                    NewStocktaking.AddStocktakingReport(Convert.ToInt32(row.Cells[2].Value), Convert.ToInt32(row.Cells[7].Value), Convert.ToInt32(row.Cells[8].Value), Convert.ToInt32(row.Cells[9].Value), Convert.ToInt32(row.Cells[3].Value));
                }
                prod.ClearWarehouse();
                DataSet NewQuantities = NewStocktaking.GetAllStocktakingProducts();
                foreach (DataRow row in NewQuantities.Tables[0].Rows)
                {
                    prod.UpdateWarehouseQuantity(Convert.ToInt32(row.ItemArray[1]), row.ItemArray[5].ToString(), Convert.ToInt32(row.ItemArray[2]));
                }
                this.Dispose();
                Program.GetMainForm.ShowControl(ControlsEnum.WAREHOUSE_CONTROL);
            }
            else MessageBox.Show("Само администратор може да затвори попис");

        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (print)
            {
                dgvStockatakingReportList.Visible = true;
                rpvStotakingReport.Visible = false;
                print = false;
            }
            else
            {
                dgvStockatakingReportList.Visible = false;
                rpvStotakingReport.Visible = true;
                print = true;

                StocktakingReportDataSet ds = new StocktakingReportDataSet();
                foreach (DataGridViewRow row in dgvStockatakingReportList.Rows)
                {
                    ds.Tables[0].Rows.Add(new object[] { row.Cells[3].Value.ToString(), row.Cells[4].Value.ToString(), row.Cells[5].Value.ToString(), row.Cells[6].Value.ToString(), row.Cells[7].Value.ToString(), row.Cells[8].Value.ToString(), row.Cells[9].Value.ToString() });
                }

                ReportParameter StocktakingDate = new ReportParameter("StocktakingDate", NewStocktaking.Date.ToString("dd-MM-yyyy"));
                ReportParameter ActiveUserPar = new ReportParameter("ActiveUser", ActiveUser.GetName() + " " + ActiveUser.GetSurname());
                ReportParameter date = new ReportParameter("Date", Today.ToString("dd-MM-yyyy"));
                
                ReportDataSource rds = new ReportDataSource("DS1", ds.Tables[0]);
                this.rpvStotakingReport.LocalReport.DataSources.Add(rds);
                this.rpvStotakingReport.LocalReport.SetParameters(StocktakingDate);
                this.rpvStotakingReport.LocalReport.SetParameters(ActiveUserPar);
                this.rpvStotakingReport.LocalReport.SetParameters(date);
                this.rpvStotakingReport.RefreshReport();
            }
        }


        private void ToStBack_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Back())
                this.Dispose();
        }


    }
}
